import { MenuResponse } from "@/services/api/user";

/** 
 * 根据path展开对应的menu列表
 * 使用深度优先搜索遍历菜单树
 */
function listMenuByPath(path: string, menus: MenuResponse[]): MenuResponse[] {
  for (const menu of menus) {
    if (menu.key === path) {
      return [menu];
    }

    if (menu.children) {
      const childMenus = listMenuByPath(path, menu.children);
      if (childMenus.length > 0) {
        return [menu, ...childMenus];
      }
    }
  }

  return [];
}

export default listMenuByPath;